package com.physique.tea.Service.Impl;

import com.physique.tea.Global.Result;
import com.physique.tea.Global.ResultList;
import com.physique.tea.Mapper.TcmSampleDataMapper;
import com.physique.tea.Service.TcmSampleDataService;
import com.physique.tea.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class TcmSampleDataServiceImpl implements TcmSampleDataService {
    @Autowired
    TcmSampleDataMapper tcmSampleDataMapper;

    public Result getTcmSampleData() {
        List<TcmSampleDataVO> tcmSampleDataMAN = tcmSampleDataMapper.getTcmSampleDataLOCAL();
        return new Result(ResultList.SUCCESS.getCode(),
                ResultList.SUCCESS.getMessage(),
                tcmSampleDataMAN);
    }

    public Result getTcmSampleDataBODY(String body) {
        List<TcmSampleDataVO> tcmSampleDataBODY = tcmSampleDataMapper.getTcmSampleDataBODY(body);
        List<String> locallist = tcmSampleDataMapper.getTcmSampleDataLOCALList();
        TcmSampleDataAndLocalListVO tcmSampleDataAndLocalListVO = new TcmSampleDataAndLocalListVO();
        tcmSampleDataAndLocalListVO.setTcmSampleDataVOList(tcmSampleDataBODY);
        tcmSampleDataAndLocalListVO.setLocallist(locallist);
        return new Result(ResultList.SUCCESS.getCode(),
                ResultList.SUCCESS.getMessage(),
                tcmSampleDataAndLocalListVO);
    }

    
    @Override
    public Result getAllPredictData() {
        List<TcmPredictDataVO> predictData = tcmSampleDataMapper.getAllPredictData();
        //记录 不同省份 的 不同性别 的 各体质的人数
        HashMap<String,HashMap<String, HashMap<String,Integer>>> hashMap = new HashMap<>();

        for(int i = 0;i<predictData.size();i++){
            TcmPredictDataVO pre = predictData.get(i);
            pre.setAge(pre.getAge().trim());
            pre.setSex(pre.getSex().trim());
            pre.setLocal(pre.getLocal().trim());
            pre.setPredictBody(pre.getPredictBody().trim());

            String local = pre.getLocal();
            String sex = pre.getSex();
            String body = pre.getPredictBody();
            if(hashMap.containsKey(local)){
                //根据地区取出映射
                HashMap<String, HashMap<String, Integer>> localMap = hashMap.get(local);
                //如果已有性别记录，取出为体质计数
                if(localMap.containsKey(sex)){
                    HashMap<String, Integer> sexMap = localMap.get(sex);
                    //取出体质计数+1,如果没有就返回1
                    sexMap.put(body,sexMap.getOrDefault(body,0)+1);
                }else{
                    //没有性别映射
                    HashMap<String,Integer> tempMap = new HashMap<>();
                    tempMap.put(body,1);

                    localMap.put(sex,tempMap);
                }
            }else{
                //如果是第一次插入该数据，从最内层开始构造
                HashMap<String,Integer> tempMap = new HashMap<>();
                tempMap.put(body,1);
                HashMap<String,HashMap<String,Integer>> tempMap1 = new HashMap<>();
                tempMap1.put(sex,tempMap);
                hashMap.put(local,tempMap1);
            }

        }

        return new Result(ResultList.SUCCESS.getCode(),
                ResultList.SUCCESS.getMessage(),
                hashMap);
    }

    @Override
    public Result getAllTeaData() {
        List<TcmTeaVO> allTeaData = tcmSampleDataMapper.getAllTeaData();
        HashMap<String, List<String>> physiqueMap = new HashMap<>();

        HashMap<String,TeaVO> teaMap = new HashMap<>();

        for (TcmTeaVO i :allTeaData){
            String physique = i.getPhysique().trim();
            if(physiqueMap.containsKey(physique)){
                List<String> list = physiqueMap.get(physique);
                list.add(i.getTea().trim());
            }else{
                ArrayList<String> arr = new ArrayList<>();
                arr.add(i.getTea().trim());
                physiqueMap.put(physique,arr);
            }
            HashMap<String,Integer> formula = new HashMap<>();
            String[] strings = i.getMaterials().split("、");
            for(int j = 0;j<strings.length-1;j++){
                // 分离名称和数值单位
                String[] split = strings[j].split("(?<=\\D)(?=\\d)");
                String name = split[0];
                String value = split[1];


                formula.put(name,Integer.parseInt(value));
                System.out.println("配方名: " + name + ", 数值: " + value );
            }

            TeaVO tea = new TeaVO(i.getMaterials().trim(),i.getEffect().trim(),
                    i.getSeason().trim(),i.getNote().trim(),i.getProperties(),formula);
            teaMap.put(i.getTea(),tea);
        }

        TeaResultVO resultVO = new TeaResultVO(physiqueMap,teaMap);
        return new Result<>(ResultList.SUCCESS.getCode(),
                ResultList.SUCCESS.getMessage(),
                resultVO);


    }
}
